home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Big Endian - Little Endian ?
- Date: 8 Mar 1996 09:12:05 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hppp5INNr2@keats.ugrad.cs.ubc.ca>
- References: <DnxL26.H7o@mail.auburn.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <DnxL26.H7o@mail.auburn.edu>,
- Bruce E. Tucker <betucker@eng.auburn.edu> wrote:
- >
- >I am in need of a Big Endian - Little Endian conversion utility in C
- >for floating point numbers.
-
- Sun Microsystems developed a standard known as XDR. There is a freely
- distributed version of it floating around.
-
- The XDR library provides filter routines for encoding basic C data types into
- external representation. It also provides an abstract data type known as an
- ``XDR stream'', through which serializing and deserializing operations take
- place. A stream can be initialized to encode/decode to a memory buffer,
- standard IO stream, or TCP/IP connection.
-
- There is also a processor known as ``rpcgen'' which translates higher-order
- structure and union definitions into recursive encoding/decoding filter
- routines which use XDR primitives to do wholesale encoding of structures. This
- interface compiler also can generate RPC stub routines and a server skeleton
- for making a simple distributed application. You just fill in the
- functionality. Though you probably just want the XDR capability which can
- easily be used on its own.
-
- In a recent project, my team has ported XDR successfully to 16-bit Windows.
- It's widely available on UNIX platforms---generally, wherever you find Sun
- NFS/NIS stuff, you will find XDR.
-
- >I have a file full of Big Endian floats and doubles and I need to be
- >able to convert these numbers for use on a Little Endian machine.
-
- If these are in IEEE format, the file could be directly treated as an XDR input
- stream, since the XDR canonical format for floating-point numbers is big-endian
- IEEE. It's possible that XDR was even used to create the file.
-
- Here is what a program to decode the file might look like:
-
-
- #include <rpc/rpc.h> /* this includes a lot of standard headers */
-
- int main(int argc, char *argv[])
-
- {
- FILE *f = fopen("datafile","r");
- XDR xstream;
-
- double data;
-
- if (!f) {
- puts("couldn't open file");
- exit(1);
- }
-
- xdrstdio_create(&xstream, f, XDR_DECODE);
-
- while(xdr_double(&xtream,&data) == TRUE) {
- /* process data */
- /* TRUE is defined in <rpc/types.h> */
- }
-
- xdr_destroy(&xtream); /* calls fflush(), not fclose() */
- fclose(f);
-
- return 0;
- }
-
-
- >Any tips, pointers, or programming examples would be appreciated.
- >Thanks in advance !
-
- Shrug. I guess my response is a little off=topic for this newsgroup.
-
-
- By the way, I just checked the FAQ. It does not mention Sun XDR and other
- techniques, though it does not address the specific problem of decoding an
- arbitrary data file, but the problem of creating a portable data file:
-
- 20.5: How can I write data files which can be read on other machines
- with different word size, byte order, or floating point formats?
-
- A: The most portable solution is to use text files (usually ASCII),
- written with fprintf() and read with fscanf() or the like.
- (Similar advice also applies to network protocols.) Be
- skeptical of arguments which imply that text files are too big,
- or that reading and writing them is too slow. Not only is their
- efficiency frequently acceptable in practice, but the advantages
- of being able to interchange them easily between machines, and
- manipulate them with standard tools, can be overwhelming.
-
- If you must use a binary format, you can improve portability,
- and perhaps take advantage of prewritten I/O libraries, by
- making use of standardized formats such as Sun's XDR (RFC 1014),
- OSI's ASN.1 (referenced in CCITT X.409 and ISO 8825 "Basic
- Encoding Rules"), CDF, netCDF, or HDF. See also questions 2.12
- and 12.38.
-
- References: PCS Sec. 6 pp. 86,88.
-
-
- --
-
-